const express = require('express');
const router = express.Router();
let response = require('../../common/responseJson');

const SsqSchema = require('../../schemas/ssq/ssqSchema');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// 篮球出现数字统计
router.get('/bluePercent', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([
    {
      '$match': match
    },{
      '$group': {
        '_id': '$lottery.blue',
        'total': {'$sum': 1}
      }
    },{
      '$sort': {
        _id: 1
      }
    }
  ]);
  promise.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return res.status(500).send(err);
  });
});

// 篮球出现列表
router.get('/blueList', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([{
    $match: match
  },{
    '$project': {
       '_id': 0,
      'number': '$lottery.blue',
      'day': '$day',
      'sequence': '$sequence',
    }
  },{
    $sort: {
      'sequence': 1
    }
  }]);
  promise.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return res.status(500).send(err);
  });
});

// 红球总和列表列表
router.get('/redSumList', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([{
    $match: match
  },{
    '$project': {
      // _id: 0,
      'blue': '$lottery.blue',
      'redSum': {'$add': ['$lottery.one', '$lottery.two', '$lottery.three', '$lottery.four', '$lottery.five', '$lottery.six']},
      'day': '$day',
      'sequence': '$sequence',
    }
  },{
    $sort: {
      'sequence': 1
    }
  }]);
  promise.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return res.status(500).send(err);
  });
});

// 红球总和列表列表
router.get('/RedSumBlueList', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([{
    $match: match
  },{
    '$project': {
      // _id: 0,
      'number': {'$add': ['$lottery.one', '$lottery.two', '$lottery.three', '$lottery.four', '$lottery.five', '$lottery.six']},
      'day': '$day',
      'sequence': '$sequence',
    }
  },{
    $sort: {
      'sequence': 1
    }
  }]);
  promise.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return res.status(500).send(err);
  });
});

// 红球出现数字统计
router.get('/redPercent', function (req, res, next) {
  let match = {};
  let day = req.query.day;
  if(day){
    match.day = {'$gte': day};
  }
  let sequence = Number(req.query.sequence);
  if(!Number.isNaN(sequence) && sequence>0){
    match.sequence = {'$gte': sequence};
  }
  let promise = SsqSchema.aggregate([
    {
      $match: match
    },{
      $unwind: '$lottery.orders'
    },{
      $group: {
        _id: '$lottery.orders',
        count: {'$sum': 1}
      }
    },{
      $sort: {
        '_id': 1
      }
    }
  ]);
  promise.then((result)=> {
    return response.success(res, result);
  }).catch((err)=> {
    return res.status(500).send(err);
  });
});

module.exports = router;
